/*
 * Created by JFormDesigner on Thu Dec 23 11:19:31 CST 2021
 */

package view;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.sun.codemodel.internal.JOp;
import model.Student;

/**
 * @author DarkFire
 */
public class MainActivity extends JFrame {
    HashSet<Student> studentHashSet = new HashSet<>();

    public MainActivity() {
        initComponents();
        setTitle("学生信息管理系统");
    }

    private void thisWindowActivated(WindowEvent e) {

    }


    private void button_SaveStudentMouseClicked(MouseEvent e) {
        //获取编辑框内容,并存入hashSet中
        String name = text_Name.getText();
        String sno = text_Sno.getText();
        String age = text_Age.getText();
        if(name.length()<1||sno.length()<1||age.length()<1){
            JOptionPane.showMessageDialog(null,"你填写的信息不全，请检查后重试！");
        }else{
            if(isContain(sno)){
                JOptionPane.showMessageDialog(null,"提交失败！学生列表已经有该学生了！");
            }else{
                studentHashSet.add(new Student(sno,name,age));
                JOptionPane.showMessageDialog(null,"提交成功！请前往学生列表查看！");
            }

        }

    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void button_RefreshMouseClicked(MouseEvent e) {
        //查询学生数据,并展示到列表中
        if (!studentHashSet.isEmpty()){
            list_Student = new JList(studentHashSet.toArray());
            scrollPane1.setViewportView(list_Student);
        }else {
            JOptionPane.showMessageDialog(null,"学生列表没数据！");
        }

    }

    private void button_QueryStudentMouseClicked(MouseEvent e) {
        String sno = text_Sno2.getText();
        //不包含输入的信息时
        Student s = searchStudent(sno);
        if (s!=null){
            JOptionPane.showMessageDialog(null,"学生学号："+sno+"\n姓名:"+s.getName()+"\n年龄："+s.getAge(),"查询成功",JOptionPane.CLOSED_OPTION);
        }else{
            JOptionPane.showMessageDialog(null,"学生列表中没有此人！","查询失败",JOptionPane.CANCEL_OPTION);
        }

    }

    boolean isContain(String sno){
        Iterator<Student> iterator = studentHashSet.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            if (s.toString().contains("学号:"+sno+" ")){
                return true;
            }
        }
        return false;
    }

    Student searchStudent(String sno){
        Iterator<Student> iterator = studentHashSet.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            System.out.println("测试");
            if (s.toString().contains("学号:"+sno+" ")){
                return s;
            }
        }
        return null;
    }

    boolean deleteStudent(String sno){
        Iterator<Student> iterator = studentHashSet.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            if (s.toString().contains("学号:"+sno+" ")){
                iterator.remove();
                return true;
            }
        }
        return false;
    }

    private void button_DeleteStudentMouseClicked(MouseEvent e) {
        String sno = text_Sno2.getText();
        if (deleteStudent(sno)){
            JOptionPane.showMessageDialog(null,"删除成功！");
        }else{
            JOptionPane.showMessageDialog(null,"查无此人！");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
        menuBar1 = new JMenuBar();
        menu1 = new JMenu();
        button_Refresh = new JButton();
        tabbedPane1 = new JTabbedPane();
        panel5 = new JPanel();
        table_Name = compFactory.createLabel("\u59d3\u540d\uff1a");
        table_SNO = compFactory.createLabel("\u5b66\u53f7:");
        table_Age = compFactory.createLabel("\u5e74\u9f84\uff1a");
        text_Name = new JTextField();
        text_Sno = new JTextField();
        text_Age = new JTextField();
        button_SaveStudent = new JButton();
        panel3 = new JPanel();
        scrollPane1 = new JScrollPane();
        list_Student = new JList();
        panel4 = new JPanel();
        table_SNO2 = compFactory.createLabel("\u5b66\u53f7:");
        text_Sno2 = new JTextField();
        button_QueryStudent = new JButton();
        button_DeleteStudent = new JButton();

        //======== this ========
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                thisWindowActivated(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {
            menuBar1.setBackground(Color.white);

            //======== menu1 ========
            {
                menu1.setText("\u529f\u80fd");

                //---- button_Refresh ----
                button_Refresh.setText("\u5237\u65b0\u6570\u636e");
                button_Refresh.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        button_RefreshMouseClicked(e);
                    }
                });
                menu1.add(button_Refresh);
            }
            menuBar1.add(menu1);
        }
        setJMenuBar(menuBar1);

        //======== tabbedPane1 ========
        {

            //======== panel5 ========
            {
                panel5.setLayout(null);
                panel5.add(table_Name);
                table_Name.setBounds(95, 40, 50, table_Name.getPreferredSize().height);
                panel5.add(table_SNO);
                table_SNO.setBounds(95, 90, 50, 16);
                panel5.add(table_Age);
                table_Age.setBounds(95, 140, 50, 16);
                panel5.add(text_Name);
                text_Name.setBounds(145, 35, 140, text_Name.getPreferredSize().height);
                panel5.add(text_Sno);
                text_Sno.setBounds(145, 85, 140, 30);
                panel5.add(text_Age);
                text_Age.setBounds(145, 130, 140, 30);

                //---- button_SaveStudent ----
                button_SaveStudent.setText("\u63d0\u4ea4");
                button_SaveStudent.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        button3MouseClicked(e);
                        button_SaveStudentMouseClicked(e);
                    }
                });
                panel5.add(button_SaveStudent);
                button_SaveStudent.setBounds(new Rectangle(new Point(175, 175), button_SaveStudent.getPreferredSize()));

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel5.getComponentCount(); i++) {
                        Rectangle bounds = panel5.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel5.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel5.setMinimumSize(preferredSize);
                    panel5.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u6dfb\u52a0\u5b66\u751f", panel5);

            //======== panel3 ========
            {
                panel3.setLayout(null);

                //======== scrollPane1 ========
                {
                    scrollPane1.setViewportView(list_Student);
                }
                panel3.add(scrollPane1);
                scrollPane1.setBounds(0, 0, 400, 220);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel3.getComponentCount(); i++) {
                        Rectangle bounds = panel3.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel3.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel3.setMinimumSize(preferredSize);
                    panel3.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u5b66\u751f\u5217\u8868", panel3);

            //======== panel4 ========
            {
                panel4.setLayout(null);
                panel4.add(table_SNO2);
                table_SNO2.setBounds(80, 55, 50, 16);
                panel4.add(text_Sno2);
                text_Sno2.setBounds(130, 50, 140, 30);

                //---- button_QueryStudent ----
                button_QueryStudent.setText("\u67e5\u8be2");
                button_QueryStudent.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        button_QueryStudentMouseClicked(e);
                    }
                });
                panel4.add(button_QueryStudent);
                button_QueryStudent.setBounds(110, 95, 78, 30);

                //---- button_DeleteStudent ----
                button_DeleteStudent.setText("\u5220\u9664");
                button_DeleteStudent.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {

                        button_DeleteStudentMouseClicked(e);
                    }
                });
                panel4.add(button_DeleteStudent);
                button_DeleteStudent.setBounds(200, 95, 78, 30);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel4.getComponentCount(); i++) {
                        Rectangle bounds = panel4.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel4.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel4.setMinimumSize(preferredSize);
                    panel4.setPreferredSize(preferredSize);
                }
            }
            tabbedPane1.addTab("\u5b66\u751f\u67e5\u8be2\u4e0e\u5220\u9664", panel4);
        }
        contentPane.add(tabbedPane1);
        tabbedPane1.setBounds(0, 0, 400, 255);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JMenuBar menuBar1;
    private JMenu menu1;
    private JButton button_Refresh;
    private JTabbedPane tabbedPane1;
    private JPanel panel5;
    private JLabel table_Name;
    private JLabel table_SNO;
    private JLabel table_Age;
    private JTextField text_Name;
    private JTextField text_Sno;
    private JTextField text_Age;
    private JButton button_SaveStudent;
    private JPanel panel3;
    private JScrollPane scrollPane1;
    private JList list_Student;
    private JPanel panel4;
    private JLabel table_SNO2;
    private JTextField text_Sno2;
    private JButton button_QueryStudent;
    private JButton button_DeleteStudent;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
